package com.gdk.ksoft.ui.works.adapters;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.constants.GoodsTypeConstant;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.ui.works.models.FworkGoodsOrderModel;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by hel on 2014-11-28.
 * 开单页面数据适配器
 */
public class PplaceOrderAdapter extends BaseAdapter {
    private Context context;
    private List<FworkGoodsOrderModel> lst;

    public PplaceOrderAdapter(Context context,List<FworkGoodsOrderModel> lst){
        this.context = context;
        this.lst = lst;
    }

    @Override
    public int getCount() {
        return lst == null ? 0:lst.size();
    }

    @Override
    public Object getItem(int position) {
        return lst.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final FworkGoodsOrderModel item = lst.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_pplaceorder_lstorder,null);
            holder.imgIcon = (ImageView)convertView.findViewById(R.id.item_pplaceorder_lstorder_imgIcon);
            holder.txtTitle = (TextView)convertView.findViewById(R.id.item_pplaceorder_lstorder_txtTitle);
            holder.txtSmallTitle = (TextView)convertView.findViewById(R.id.item_pplaceorder_lstorder_txtSmallTitle);
            holder.txtContent = (TextView)convertView.findViewById(R.id.item_pplaceorder_lstorder_txtContext);
            holder.txtSmallContent = (TextView)convertView.findViewById(R.id.item_pplaceorder_lstorder_txtSmallContext);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        new BitmapUtils(context).configDefaultLoadFailedImage(R.drawable.ic_default_goods)
                .configDefaultLoadingImage(R.drawable.ic_default_goods)
                .display(holder.imgIcon, SystemData.BASE_URL + item.goodsInfo.slt);
        holder.txtTitle.setText(item.goodsInfo.mc);
        String goodsType =  GoodsTypeConstant.getGoodsType().get(item.goodsType).trim();
        holder.txtSmallTitle.setText(Html.fromHtml(item.goodsInfo.gg+item.goodsInfo.dw
                +(goodsType.equals("正常")?"":"<font color='red'>("+goodsType+")</font>")));
        holder.txtContent.setText(item.goodsInfo.tm);
        holder.txtSmallContent.setText((item.pieceNum==0?"":item.pieceNum+item.goodsModuleInfo.jldwmc)+(item.singleNum==0?"":item.singleNum
                +item.goodsInfo.dw)+"*"+FormatUtils.exactNumber(item.finalPrice,"0.00")+"="+ FormatUtils.exactNumber(item.totalMoney,"0.00")+"元");
        return convertView;
    }

    /**
     * 获取列表中的所有数据列
     * @return
     */
    public List<FworkGoodsOrderModel> getList(){
        return this.lst;
    }

    class ViewHolder{
        public ImageView imgIcon;
        public TextView txtTitle,
                        txtSmallTitle,
                        txtContent,
                        txtSmallContent;
    }

}
